/**
 * This class contains the GUI components of the Applet.
 * @author David Emole
 */
package com.spell;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;

@SuppressWarnings("serial")
public class SpellApplet extends JApplet {
	/******Defining UI components**/
	JPanel mainPanel = new JPanel();
	JPanel midPanel = new JPanel();
	JPanel topPanel = new JPanel();
	JLabel label1 = new JLabel("Enter a number ");
	JLabel numberLabel = new JLabel();
	JTextField numberBox = new JTextField();
	JButton spellButton = new Bluebutton("Spell It Out");
	JButton clearButton = new Bluebutton("Clear");
	JTextPane answerBox = new JTextPane();
	JScrollPane scroller = new JScrollPane(answerBox);
	String defaultFont = label1.getFont().getName();

	/**
	 * Creates the applet's GUI.
	 * Specifies the functions and arrangements of the components
	 */
	public SpellApplet(){
		this.setContentPane(mainPanel);
		this.getRootPane().setDefaultButton(spellButton);
		
		/********Arrangement**********************/
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
		mainPanel.add(topPanel);
		mainPanel.add(midPanel);
		mainPanel.add(scroller);
		
		midPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
		midPanel.add(numberLabel);
		
		topPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
		topPanel.add(label1);
		topPanel.add(numberBox);
		topPanel.add(spellButton);
		topPanel.add(clearButton);

		/*************Button Actions************/
		spellButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				takeUserInputAndSpellIt();
			}
		});
		clearButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				answerBox.setText("");
				numberBox.setText("");
				numberLabel.setText("");
			}
		});
		
		/************** Cosmetics******************/
		int bw = 1; //border width
		int bs = 5; //border spacing
		Color bdc = Color.BLUE; //border color
		Color bkc = Color.WHITE; //background color
		
		numberBox.setPreferredSize(new Dimension(300, 30));
		answerBox.setPreferredSize(new Dimension(550, 150));
		answerBox.setEditable(false);
		scroller.setBorder(null);
		numberBox.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createLineBorder(bdc, bw), BorderFactory.createEmptyBorder(bs, bs, bs, bs)));
		midPanel.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createMatteBorder(bw, 0, 0, 0, bdc), BorderFactory.createEmptyBorder(bs, bs, bs, bs)));
		mainPanel.setBorder(BorderFactory.createLineBorder(bdc, bw));
		mainPanel.setBackground(bkc);
		topPanel.setBackground(bkc);
		midPanel.setBackground(bkc);
		numberLabel.setForeground(new Color(100,100,255));
		numberLabel.setFont(new Font(defaultFont, Font.PLAIN, 14));
		label1.setForeground(bdc);
		label1.setFont(new Font(defaultFont, Font.PLAIN, 16));
		answerBox.setFont(new Font(defaultFont, Font.PLAIN, 18));
		numberBox.setFont(new Font(defaultFont, Font.PLAIN, 14));
	}
	
	/**
	 * Get the user's input from the textbox. Get its spelling using the NumberSpeller class' spell function.
	 * Write the spelling in the appropriate textpane. Format the textpane's content using the TextFormatter class.
	 */
	public void takeUserInputAndSpellIt(){
		String input = numberBox.getText().trim();
		
		try {
			NumValidator.validateStringAsNumber(input);
			input = SpellUtils.normalize(input);
			
			PartitionedNumber inputnum = new PartitionedNumber(input);
			answerBox.setText(inputnum.getSpelling());
			numberLabel.setText(inputnum.getReadableNumber());
		} catch(NumberFormatException e){
			answerBox.setText(SpellConstants.INVALID_TAG + ": " + e.getMessage());
			numberLabel.setText("");
		}catch (Exception e1) {
			//There should not be any unhandled case but just in case it happens, 
			//program shouldnt crash.
			answerBox.setText("An unexpected error has occured. \r\n\r\n"+ e1.toString()
			+ "\r\n" + Arrays.deepToString(e1.getStackTrace()));
			numberLabel.setText("");
		}	

		TextFormatter.format(answerBox);
		answerBox.setCaretPosition(0);
	}

	public void init(){
		this.setSize(new Dimension(560, 180));
		
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) { }
	}

	/**
	 * Custom button.
	 * @author David Emole
	 */
	class Bluebutton extends JButton implements MouseListener{
		Color outColor = new Color(100,100,255);
		Color inColor = new Color(200,200,255);
		
		public Bluebutton(String text){
			super(text);
			this.addMouseListener(this);
			this.setBackground(outColor);
			this.setForeground(Color.WHITE);
			this.setFont(new Font(defaultFont, Font.PLAIN, 14));
			this.setFocusable(false);
			int x=4; //border spacing
			this.setBorder(BorderFactory.createEmptyBorder(x, x, x, x));
			this.setPreferredSize(new Dimension(this.getPreferredSize().width, 30));
		}

		public void mouseEntered(MouseEvent arg0) {
			this.setBackground(inColor);
			this.setCursor(new Cursor(Cursor.HAND_CURSOR));
		}

		public void mouseExited(MouseEvent arg0) {
			this.setBackground(outColor);
			this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
		}

		public void mouseClicked(MouseEvent arg0) {}
		public void mousePressed(MouseEvent arg0) {}
		public void mouseReleased(MouseEvent arg0) {}
	}
}
